package com.nlp.fresh_comp.utils;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * zoopool 工程工具类
 * 
 * 函数： public static Set<File> folderWalk(String path)；
 * 
 * @author lixuze
 * 
 */

public class FileUtils {

	/**
	 * 功能:获得一个路径下的所有文件 思想:递归 附加:无
	 * 
	 * @param path
	 *            字符串 要获得路径的文件
	 * @return
	 */
	public static Set<File> folderWalk(String path) {
		File folder = new File(path);
		Set<File> fileList = new HashSet<File>();
		for (File file : folder.listFiles()) {
			if (file.isDirectory()) {
				fileList.addAll(folderWalk(file.getAbsolutePath()));
			} else {
				fileList.add(file);
			}
		}
		return fileList;
	}

	/**
	 * @functionnamepublic static List<String> readFileData(File file) throws
	 *                     Exception;
	 * 
	 * @function:：读取文件 函数思想： 文件读取
	 * 
	 * 
	 * @param file
	 * @return
	 * @throws Exception
	 */
	public static List<String> readFileData(File file) throws Exception {
		List<String> fileDataList = new ArrayList<String>();
		BufferedReader fileReader = null;
		try {
			fileReader = new BufferedReader(new InputStreamReader(
					new FileInputStream(file)));
			String data = null;
			while ((data = fileReader.readLine()) != null) {
				fileDataList.add(data);
			}
		} catch (Exception e) {
			throw new Exception("FILE_READ_FAILED", e);
		} finally {
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					throw new Exception("FILE_CLOSE_FAILED", e);
				}
			}

		}
		return fileDataList;
	}
	
	public static List<String> readFileData(InputStream input) throws IOException {
		List<String> contents = new ArrayList<String>();
		BufferedReader br = new BufferedReader(new InputStreamReader(input));
		String line = null;
		while (br.ready()) {
			line = br.readLine().trim();

			if (line != null && line.isEmpty() != true) { // && line.length()>1
				contents.add(line);
			}
		}

		br.close();
		return contents;
	}
	/**
	 * @functionname public static List<String> readFileData(String filePath)
	 *               throws Exception；
	 * 
	 * @function: 文件读取
	 * 
	 * @param filePath
	 * @return
	 * @throws Exception
	 */
	public static List<String> readFileData(String filePath) throws Exception {
		File file = new File(filePath);
		return readFileData(file);
	}

	/**
	 * @functionname public static long getElementString(String word, String
	 *               splitString)；
	 * @function:： 字符串 word ，在分割符下切分之后数组长度
	 * 
	 * @param word
	 * @param splitString
	 * @return
	 */
	public static int getElementString(String word, String splitString) {
		int reCount = 0;
		if (!isEmpty(word)) {
			reCount = word.split(splitString).length;
		}
		return reCount;
	}

	public static boolean isEmpty(String str) {
		return (str == null || str.isEmpty()) ? true : false;
	}

	/***
	 * @functionname： public static String wordListToString(List<String>
	 *                wordList, String splitWord)；
	 * 
	 * @function： 将字符串list转换为String通过分割副 函数思想： 无
	 * 
	 * @param wordList
	 *            wordList
	 * @param splitWord
	 *            分割符
	 * @return
	 */
	public static String wordListToString(List<String> wordList,
			String splitWord) {
		StringBuffer stringBuffer = new StringBuffer();
		boolean isFirst = true;
		for (String word : wordList) {
			if (isFirst) {
				stringBuffer.append(word);
				isFirst = false;
			} else {
				stringBuffer.append(" " + word);
			}
		}
		return stringBuffer.toString();
	}

	public static void writeFile(File file, List<String> contents)
			throws IOException {
		FileWriter fw = null;
		fw = new FileWriter(file);
		for (String sentence : contents) {
			fw.write("未知\t"+sentence + "\n");
		}
		if (fw != null) {
			fw.close();
		}
	}

	public static void writeFile(String fileName, List<String> contents)
			throws IOException {
		writeFile(new File(fileName), contents);
	}

	public static void writeFile(String fileName, String content)
			throws IOException {
		List<String> contents = new ArrayList<String>();
		contents.add(content);
		writeFile(new File(fileName), contents);
	}
	/**
	 * 将文件平均分割为多分
	 * @param path:文件路径
	 * @param filenName：待分割文件
	 * @param sub：文件分割分数
	 * @param outName:输出文件名
	 * @return
	 * @throws IOException 
	 */
	public static boolean splitFile(String path, String filenName, String outName, int sub ) {
		try {
			BufferedReader fileReader  = new BufferedReader(new InputStreamReader(
					new FileInputStream(new File( path + filenName ))) );
			
			FileWriter[] fw = new FileWriter[ sub ];
			for( int id = 1; id <= sub; id ++ ){
				fw[ id - 1] = new FileWriter( new File(path + outName +  ".sub" + id ));
			}
			String data = null;
			int random = -1;
			while ((data = fileReader.readLine()) != null) {
				random = (int)(Math.random() * sub );
				fw[ random ].write( data + "\n");
			}
			fileReader.close();
			for( int id = 1; id <= sub; id ++ ){
				fw[ id - 1 ].close();
			}
			return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			return false;
		}
	}
	/**
	 * @function 切割字符串，通过分割符号
	 * 
	 * 
	 * @param sentence
	 * @param splitWordSet
	 * @return
	 */
	public static List<String> splitWordBy(String sentence,
			Set<Character> splitWordSet) {
		// 简单的字符串比较算法
		// 利用set hash 增加查找速度
		List<String> strSplitList = new ArrayList<String>();
		if (!isEmpty(sentence)) {
			int splitStart = -1; // 如果设置为0的话，第一个词会少哥字符
			for (int i = 0; i < sentence.length(); i++) { // 循环字符串
				if (splitWordSet.contains(sentence.charAt(i))) { // 如果出现分隔符，处理
					if (splitStart != (i - 1)) { // 看是否会出现这样的情况 ，。，，多个分割符号情况
						String subStr = sentence.substring(splitStart + 1, i) // 切割字符串
								.trim();
						if (!isEmpty(subStr)) {
							strSplitList.add(subStr);
						}
					}
					splitStart = i; // 保存上次出现分割符号的位置
				}
			}
			if(splitStart != sentence.length() - 1){
				String subStr = sentence.substring(splitStart + 1, sentence.length()).trim();
				strSplitList.add(subStr);
			}
		}
		return strSplitList;
	}

	/**
	 * @function 分割字符串通过 splitwordArry
	 * 
	 * @param sentence
	 * @param splitWordArry
	 * @return
	 */
	public static List<String> splitWordBy(String sentence, char[] splitWordArry) {
		List<String> strSplitList = new ArrayList<String>();
		// 分割符号数组 可以用二分法进行处理
		if (!isEmpty(sentence)) {
			int splitStart = -1;
			for (int i = 0; i < sentence.length(); i++) {
				boolean isFind = false;
				char word = sentence.charAt(i);
				for (int j = 0; j < splitWordArry.length; j++) {
					if (word == splitWordArry[j]) {
						isFind = true;
					}
				}
				if (isFind) {
					if (splitStart != (i - 1)) {
						String subStr = sentence.substring(splitStart + 1, i)
								.trim();
						if (!isEmpty(subStr)) {
							strSplitList.add(subStr);
						}
					}
					splitStart = i;
				}
			}
		}
		return strSplitList;
	}

	public static List<String> addPrex(String prex, List<String> contents) {
		List<String> prexList = new ArrayList<String>();
		for (String line : contents) {
			line = prex + line;
			prexList.add(line);
		}
		return prexList;

	}

	public static void main(String args[]) {

		Set<Character> stops = new HashSet<Character>();
		stops.add('.');
		stops.add('。');
		stops.add('！');
		stops.add('!');
		stops.add('?');
		stops.add('？');
		stops.add(';');
		stops.add('；');
		stops.add('】');
		stops.add('【');
		stops.add('，');
		stops.add(',');
		try {
			List<String> contents = readFileData("/home/zyl/car/cardata/good.seg");
			List<String> fileContent = new ArrayList<String>();
			String basePath = "/home/zyl/car/cardata/good.seg.txt";
			for (String line : contents) {
				//String lineArry[] = line.split("\t");
				fileContent.addAll(splitWordBy(line, stops));
				
			}
			writeFile(basePath, fileContent);
			fileContent.clear();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}



}